import {defineStore} from "pinia"
import {routes} from "@/router"

export const useManegerStore=defineStore("maneger",{
    state :()=>{
        return {
            manegerData:{},
            detailsData:[]
        }
    },
    getters:{
        permissionRoutes(state){
            const arr=routes.filter((item)=>{
                return !item.hidden
            })
            if(state.role===2){
                //超级管理员默认拥有所有权限
                return arr
            }else{
                //普通用户
                return filterRoutes(arr,state.manegerData.checkedkeys)
            }
        }
    },
    actions:{
        setManegerData(data){
            
            this.manegerData=data
            //console.log(this.manegerData);
        },
        setDetailsData(data){
            this.detailsData=data
            console.log("setDetailsData",data);
        }
    },
    persist:true
})

function filterRoutes(routes,roles){
    let result=[]
    routes.forEach((route)=>{
        const tmp={...route}
        if(roles.includes(tmp.name)){
            if(tmp.children){
                tmp.children=filterRoutes(tmp.children,roles)
            }
            result.push(tmp)
        }
    })
    return result
}

